package org.nowcoder.leetcode.string;

/**
 * Title  : 14. Longest Common Prefix
 * Source : https://leetcode.cn/problems/longest-common-prefix/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-01
 */

public class LeetCode_14 {
    public String longestCommonPrefix(String[] strs) {
        //暴力解法： 纵向扫描
        StringBuilder builder = new StringBuilder();
        if (strs.length > 1) {
            int count = 0;
            while (count < strs[0].length()) {
                char c = strs[0].charAt(count);
                for (int i = 1; i < strs.length; i++) {
                    if (count == strs[i].length() || c != strs[i].charAt(count)) {
                        return builder.toString();
                    }
                }
                builder.append(c);
                count++;
            }
        } else {
            if (strs.length == 1) {
                builder.append(strs[0]);
            }
        }
        return builder.toString();
    }
}
